package com.monochromebytes.mononet.virtual;

import java.nio.ByteBuffer;

public class ConnectionID {

    public static final int  LENGTH    = Short.SIZE / 8;
    
    private final ByteBuffer buffer;

    public ConnectionID(ByteBuffer buffer) {
        assert (buffer.capacity() >= LENGTH); // TODO
        this.buffer = buffer.slice();
    }

    public ConnectionID(int value) {
        this.buffer = ByteBuffer.allocate(ConnectionID.LENGTH);
        this.buffer.putShort(0, (short) value);
    }

    public void set(ConnectionID other) {
        this.buffer.duplicate().put(other.buffer.duplicate());
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((buffer == null) ? 0 : buffer.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        ConnectionID other = (ConnectionID) obj;
        if (buffer == null) {
            if (other.buffer != null) {
                return false;
            }
        }
        else if (!buffer.equals(other.buffer)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return ""+buffer.getShort(0);
    }
    
}
